package com.example.springbootlearn.concurrent;

import javax.sound.midi.Soundbank;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author caiji Mr. Li
 * @date 2019/10/23 21:07
 */
public class JoinCountDownLatch  {

    private static CountDownLatch countDownLatch =new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException{
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(()->{
//            Thread.sleep(1000);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                countDownLatch.countDown();
            }
            System.out.println("child threadOne over");
        });

        executorService.submit(()->{
//            Thread.sleep(1000);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                countDownLatch.countDown();
            }
            System.out.println("child threadTwo over");
        });

        System.out.println("wait all child thread over!");
        countDownLatch.await();

        System.out.println("all child thread over!");
        executorService.shutdown();
    }
}
